{{template "shared/bootstrap_table_js.html"}}
<script>
    var $dataGrid = $('#dataGrid');
    var $searchForm = $('#searchForm');

    $(function () {
        pageInit()
    });

    function pageInit() {
        dataGridInit()
    }

    function dataGridInit() {
        $dataGrid.bootstrapTable({
            url: '{{urlfor "RegisterUserController.DataGrid"}}',
            method: 'post',
            sidePagination: 'server',
            idField: 'Id',
            queryParamsType: 'limit',
            queryParams: function (params) {
                params.Mobile = $.trim($('.searchForm input[name="Mobile"]').val())
                params.RealName = $.trim($('.searchForm input[name="RealName"]').val())
                return params;
            },
            pagination: true,
            showRefresh: true,
            showColumns: true,
            toolbar: '#toolbar',
            pageSize: 10,
            pageList: [5, 10, 20, 100, 200],
            paginationShowPageGo: true,//20170812 lht 扩展select跳转
            paginationUseBSSelect: true,//20170812 lht 扩展select跳转 启用BoostrapSelect(页面须引用botstrap-select.min.js)
            cookie: true,
            classes: 'table table-bordered table-hover',
            undefinedText: '',
            sortName: 'Id',
            sortOrder: 'desc',
            stickyHeader: true,
            resizable: true,

            rowAttributes: function (row, index) {
                return {'data-pk': row.Id}
            },
            columns: [{
                field: 'state',
                checkbox: true,
                radio: false
            }, {
                field: 'Id',
                title: 'ID',
                width: '80px',
                align: 'center',
                sortable: true
            }, {
                field: 'Nickname',
                title: '微信昵称'
            }, {
                field: 'Mobile',
                title: '手机号码'
            }, {
                field: 'RealName',
                title: '姓名'
            }, {
                field: 'LoginTime',
                title: '上次登录时间',
                formatter: function (value, row, index) {
                    return new Date(value * 1000).format('yyyy-MM-dd hh:mm:ss')
                }
            }, {
                field: 'operate',
                title: '操作',
                align: 'center',
                width: '100px',
                visible: function () {
                    if ('{{.canEdit}}' === 'true') {
                        return true
                    }
                    return false
                },
                formatter: function (value, row, index) {
                    var html = [];
                    html.push('<div class="btn-group">');
                    if ('{{.canEdit}}' === 'true') {
                        html.push('<button class="btn btn-primary btn-xs" onclick="EditFun(' + row.Id + ')">' +
                                '<i class="fa fa-pencil"></i> 编辑</button>');
                    }
                    html.push('</div>');
                    return html.join('');
                },
                onLoadSuccess: function (data) {
                    layer.closeAll('loading')
                },
                onSort: function (name, order) {

                },
                onCheckAll: function (rows) {
                    $(".table-btn-check-enable").removeAttr("disabled")
                },
                onCheck: function (rows) {
                    $(".table-btn-check-enable").removeAttr('disabled');
                },
                onUncheck: function (rows) {
                    if ($("#dataGrid").bootstrapTable("getSelections").length === 0) {
                        $(".table-btn-check-enable").attr('disabled', 'disabled');
                    }
                },
                onUncheckAll: function () {
                    //禁用删除需要勾选才能用的按钮
                    if ($("#dataGrid").bootstrapTable("getSelections").length === 0) {
                        $(".table-btn-check-enable").attr('disabled', 'disabled');
                    }
                },
                onLoadError: function (status) {
                    layer.alert('获取数据失败,错误代码：' + status);
                }
            }],
        });
    }

    function EditFun(Id) {
        var title = "添加用户";
        if (Id > 0) {
            title = '编辑用户';
        }
        var url = '{{ urlfor "RegisterUserController.Edit"}}' + '/' + Id;
        layer.open({
            type: 2,
            title: title,
            shadeClose: false,
            shade: 0.2,
            maxmin: true,
            shift: 1,
            area: ['1000px', '600px'],
            content: url,
            btn: ['保存', '关闭'],
            yes: function (index, layero) {
                var iframeWin = window[layero.find('iframe')[0]['name']];
                iframeWin.FormSubmit();
            }
        });
    }

    function DeleteFun() {
        var url = '{{ urlfor "RegisterUserController.Delete"}}';
        var rows = $dataGrid.bootstrapTable("getSelections")
        if (rows.length === 0) {
            layer.alert('请选择想要删除的项', {icon: 0, title: '提示'});
            return;
        }

        var ids = [];
        $(rows).each(function (i, e) {
            ids.push(e.Id);
        });

        layer.confirm('您是否要删除所选的项？', {
            btn: ['是', '否'], icon: 3, title: '请确认'
        }, function () {
            var params = {ids:ids.join(',')};
            $.sdpost(url, params, function (re) {
                if (re.code == 0) {
                    layer.msg(re.msg, {icon: 1, title: '成功'});
                    refresh();
                } else {
                    layer.alert(re.msg, {icon: 2, title: "失败"});
                }
            });
        });
    }

    Date.prototype.Format = function (fmt) {
        var o = {
            "M+": this.getMonth() + 1, // 月份
            "d+": this.getDate(), // 日
            "h+": this.getHours(), // 小时
            "m+": this.getMinutes(), // 分
            "s+": this.getSeconds(), // 秒
            "q+": Math.floor((this.getMonth() + 3) / 3), // 季度
            "S": this.getMilliseconds() // 毫秒
        };
        if (/(y+)/.test(fmt))
            fmt = fmt.replace(RegExp.$1, (this.getFullYear() + ""));
        for (var k in o)
            if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
        return fmt;
    }
</script>